if get_option('progs').allowed()

  _m_dep = cc.find_library(
    'm',
    required: false,
  )

  foreach _spec : [
    ['adaptmap_dark'],
    ['arabic_lines'],
    ['arithtest'],
    ['autogentest1'],
    ['autogentest2'         , false, 'autogen.137.c'],
    ['barcodetest'],
    ['binarize_set'],
    ['binarizefiles'],
    ['bincompare'],
    ['blendcmaptest'],
    ['buffertest'],
    ['ccbordtest'],
    ['cctest1'],
    ['cleanpdf'],
    ['colorsegtest'],
    ['comparepages'],
    ['comparepixa'],
    ['comparetest'],
    ['compresspdf'],
    ['contrasttest'],
    ['convertfilestopdf'    , true],
    ['convertfilestops'     , true],
    ['convertformat'        , true],
    ['convertsegfilestopdf' , true],
    ['convertsegfilestops'  , true],
    ['converttogray'],
    ['converttopdf'         , true],
    ['converttops'          , true],
    ['cornertest'],
    ['corrupttest'],
    ['croppdf'],
    ['croptext'],
    ['deskew_it'],
    ['dewarprules'],
    ['dewarptest1'],
    ['dewarptest2'],
    ['dewarptest3'],
    ['dewarptest4'],
    ['dewarptest5'],
    ['digitprep1'],
    ['displayboxa'],
    ['displayboxes_on_pixa'],
    ['displaypix'],
    ['displaypixa'],
    ['dwalineargen'],
    ['fcombautogen'],
    ['fhmtautogen'],
    ['fileinfo'             , true],
    ['find_colorregions'],
    ['findbinding'],
    ['findpattern1'],
    ['findpattern2'],
    ['findpattern3'],
    ['fmorphautogen'],
    ['fpixcontours'],
    ['gammatest'],
    ['graphicstest'],
    ['graymorphtest'],
    ['hashtest'],
    ['histoduptest'],
    ['histotest'],
    ['htmlviewer'],
    ['imagetops'            , true],
    ['jbcorrelation'],
    ['jbrankhaus'],
    ['jbwords'],
    ['lightcolortest'],
    ['listtest'],
    ['livre_adapt'],
    ['livre_hmt'],
    ['livre_makefigs'],
    ['livre_orient'],
    ['livre_pageseg'],
    ['livre_seedgen'],
    ['livre_tophat'],
    ['maketile'],
    ['maptest'],
    ['messagetest'],
    ['misctest1'],
    ['modifyhuesat'],
    ['morphtest1'],
    ['numaranktest'],
    ['otsutest1'],
    ['otsutest2'],
    ['pagesegtest1'],
    ['pagesegtest2'],
    ['partifytest'],
    ['partitiontest'],
    ['percolatetest'],
    ['pixaatest'],
    ['pixafileinfo'],
    ['plottest'],
    ['printimage'],
    ['printsplitimage'],
    ['printtiff'],
    ['rasteroptest'],
    ['rbtreetest'],
    ['recog_bootnum1'],
    ['recog_bootnum2'],
    ['recog_bootnum3'],
    ['recogsort'],
    ['recogtest1'],
    ['recogtest2'],
    ['recogtest3'],
    ['recogtest4'],
    ['recogtest5'],
    ['recogtest6'],
    ['recogtest7'],
    ['reducetest'],
    ['removecmap'],
    ['renderfonts'],
    ['replacebytes'],
    ['rotate_it'],
    ['rotatefastalt'],
    ['rotateorthtest1'],
    ['rotatetest1'],
    ['runlengthtest'],
    ['scale_it'],
    ['scaleandtile'],
    ['scaletest1'],
    ['scaletest2'],
    ['seedfilltest'],
    ['settest'],
    ['sharptest'],
    ['sheartest'],
    ['showedges'],
    ['skewtest'],
    ['sorttest'],
    ['splitimage2pdf'],
    ['splitpdf'],
    ['sudokutest'],
    ['textorient'],
    ['tiffpdftest'],
    ['trctest'],
    ['underlinetest'],
    ['warpertest'],
    ['wordsinorder'],
    ['writemtiff'],
    ['xtractprotos'         , true],
    ['yuvtest'],
  ]
    _name = _spec[0]
    _install = _spec.get(1, false)
    _extra_srcs = _spec.get(2, '').split()
    _exe = executable(
      _name,
      [_name + '.c'] + _extra_srcs,
      c_args: '-DLIBLEPT_IMPORTS',
      dependencies: [lept_dep, _m_dep],
      install: _install,
    )
  endforeach

endif

if get_option('tests').require(
  host_machine.system() != 'darwin',
  error_message: 'running the testsuite on macOS is unsupported',
).allowed()

  has_gnuplot = find_program(
    'gnuplot',
    required: false,
  ).found()

  _sh = find_program('sh')
  _m_dep = cc.find_library(
    'm',
    required: false,
  )

  foreach _spec : [
    ['alltests_reg'         , ''                               , true],
    ['adaptmap_reg'         , 'libjpeg libpng'],
    ['adaptnorm_reg'        , 'libjpeg'],
    ['affine_reg'           , 'libjpeg libtiff'],
    ['alphaops_reg'         , 'libjpeg libpng libtiff'],
    ['alphaxform_reg'       , 'libjpeg'],
    ['baseline_reg'         , 'gnuplot libjpeg libpng'],
    ['bilateral1_reg'       , 'libjpeg libpng'                 , true],
    ['bilateral2_reg'       , 'libjpeg'],
    ['bilinear_reg'         , 'libjpeg libtiff'],
    ['binarize_reg'         , 'libjpeg'],
    ['binmorph1_reg'        , 'libtiff'],
    ['binmorph2_reg'        , 'libtiff'                        , true],
    ['binmorph3_reg'        , 'libtiff'],
    ['binmorph4_reg'        , 'libtiff'                        , true],
    ['binmorph5_reg'        , 'libtiff'                        , true],
    ['binmorph6_reg'        , 'libtiff'],
    ['blackwhite_reg'       , 'libpng'],
    ['blend1_reg'           , 'libjpeg libpng'],
    ['blend2_reg'           , 'libjpeg libpng'],
    ['blend3_reg'           , 'libjpeg libpng libtiff'],
    ['blend4_reg'           , 'libjpeg libpng libtiff'],
    ['blend5_reg'           , 'libjpeg libpng'],
    ['boxa1_reg'            , 'gnuplot'],
    ['boxa2_reg'            , 'gnuplot libpng'],
    ['boxa3_reg'            , 'gnuplot libpng libtiff'],
    ['boxa4_reg'            , 'gnuplot libjpeg libpng libtiff'],
    ['bytea_reg'            , 'libjpeg'],
    ['ccbord_reg'           , 'libpng libtiff libz'],
    ['ccthin1_reg'          , 'libtiff libz'],
    ['ccthin2_reg'          , 'libpng libtiff libz'],
    ['checkerboard_reg'     , 'libtiff'],
    ['circle_reg'           , 'libpng'],
    ['cmapquant_reg'        , 'libjpeg'],
    ['colorcontent_reg'     , 'libjpeg libz'],
    ['colorfill_reg'        , 'libjpeg'],
    ['coloring_reg'         , 'libpng'],
    ['colorize_reg'         , 'libjpeg'],
    ['colormask_reg'        , 'gnuplot libpng'],
    ['colormorph_reg'       , 'libjpeg'],
    ['colorquant_reg'       , 'libjpeg'],
    ['colorseg_reg'         , 'libjpeg'],
    ['colorspace_reg'       , 'libjpeg gnuplot libpng'],
    ['compare_reg'          , 'libjpeg libpng libtiff libz'],
    ['compfilter_reg'       , 'libtiff'],
    ['conncomp_reg'         , 'libpng libtiff'],
    ['conversion_reg'       , 'libjpeg libpng libtiff'],
    ['convolve_reg'         , 'libjpeg libpng libtiff'],
    ['crop_reg'             , 'gnuplot libjpeg libpng'],
    ['dewarp_reg'           , 'libjpeg'],
    ['distance_reg'         , 'libtiff'],
    ['dither_reg'           , 'libjpeg'],
    ['dna_reg'              , 'gnuplot libpng'],
    [
      'dwamorph1_reg'        ,
      'libtiff'                        ,
      false,
      'dwalinear.3.c dwalinearlow.3.c',
    ],
    [
      'dwamorph2_reg'        ,
      'gnuplot libpng libtiff'         ,
      true,
      'dwalinear.3.c dwalinearlow.3.c',
    ],
    ['edge_reg'             , 'libjpeg'],
    ['encoding_reg'         , 'libjpeg libpng libz'],
    ['enhance_reg'          , 'gnuplot libjpeg libpng'],
    ['equal_reg'            , 'libpng libjpeg libtiff'],
    ['expand_reg'           , 'libjpeg libpng'],
    ['extrema_reg'          , 'gnuplot libpng'],
    ['falsecolor_reg'       , ''],
    ['fhmtauto_reg'         , 'libtiff'],
    ['files_reg'            , ''                               , true],
    ['findcorners_reg'      , 'libtiff'],
    ['findpattern_reg'      , 'libpng'],
    ['flipdetect_reg'       , 'libtiff'],
    ['fmorphauto_reg'       , ''                               , true],
    ['fpix1_reg'            , 'gnuplot libjpeg libpng'],
    ['fpix2_reg'            , 'libjpeg'],
    ['genfonts_reg'         , 'libpng libtiff'],
    ['gifio_reg'            , 'libgif'],
    ['grayfill_reg'         , ''],
    ['graymorph1_reg'       , 'libjpeg'],
    ['graymorph2_reg'       , 'libjpeg'],
    ['grayquant_reg'        , 'libjpeg libtiff'],
    ['hardlight_reg'        , 'libjpeg'],
    ['hash_reg'             , 'gnuplot libjpeg'],
    ['heap_reg'             , ''],
    ['insert_reg'           , 'libpng libtiff'],
    ['ioformats_reg'        , 'libjpeg libpng libtiff'],
    ['iomisc_reg'           , 'libjpeg libpng libtiff'],
    ['italic_reg'           , 'gnuplot libpng libz'],
    ['jbclass_reg'          , 'libtiff'],
    ['jp2kio_reg'           , 'libjp2k'],
    ['jpegio_reg'           , 'libjpeg libpng'],
    ['kernel_reg'           , 'gnuplot libjpeg libpng libtiff'],
    ['label_reg'            , 'libtiff'],
    ['lineremoval_reg'      , 'libpng'],
    ['locminmax_reg'        , 'libjpeg'],
    ['logicops_reg'         , 'libjpeg libpng'],
    ['lowaccess_reg'        , 'libtiff'],
    ['lowsat_reg'           , 'libjpeg'],
    ['maze_reg'             , 'libjpeg'],
    ['morphseq_reg'         , 'libjpeg libpng libtiff libz'    , true],
    ['mtiff_reg'            , 'libpng libtiff'],
    ['multitype_reg'        , 'libtiff'],
    ['nearline_reg'         , 'gnuplot libpng libtiff'],
    ['newspaper_reg'        , 'libtiff'],
    ['numa1_reg'            , 'gnuplot libjpeg libpng'],
    ['numa2_reg'            , 'gnuplot libjpeg libpng'],
    ['numa3_reg'            , 'gnuplot libjpeg libpng'],
    ['overlap_reg'          , ''],
    ['pageseg_reg'          , 'libjpeg libpng libtiff libz'],
    ['paint_reg'            , 'libjpeg libpng libtiff'],
    ['paintmask_reg'        , 'libjpeg libpng libtiff'],
    ['partition_reg'        , 'libjpeg libtiff'],
    ['pdfio1_reg'           , 'libjpeg libpng libtiff libz'],
    ['pdfio2_reg'           , 'libjpeg libpng libz'],
    ['pdfseg_reg'           , 'libjpeg libpng'],
    ['pixa1_reg'            , 'gnuplot libpng'],
    ['pixa2_reg'            , 'libjpeg libtiff'],
    ['pixadisp_reg'         , 'libjpeg libtiff'],
    ['pixalloc_reg'         , 'libjpeg libtiff'                , true],
    ['pixcomp_reg'          , 'libjpeg libpng libtiff'],
    ['pixmem_reg'           , 'libjpeg libpng libtiff'],
    ['pixserial_reg'        , 'libjpeg libpng libtiff'],
    ['pixtile_reg'          , 'libjpeg'                        , true],
    ['pngio_reg'            , 'libjpeg libpng libtiff'],
    ['pnmio_reg'            , 'libjpeg libpng libtiff'],
    ['projection_reg'       , 'gnuplot libjpeg libpng libtiff'],
    ['projective_reg'       , 'libjpeg libtiff'],
    ['psio_reg'             , 'libjpeg libpng libtiff libz'],
    ['psioseg_reg'          , 'libtiff'],
    ['pta_reg'              , 'libtiff'],
    ['ptra1_reg'            , 'libtiff'],
    ['ptra2_reg'            , 'libpng libtiff'],
    ['quadtree_reg'         , 'libpng'],
    ['rank_reg'             , 'gnuplot libjpeg'],
    ['rankbin_reg'          , 'gnuplot libjpeg libpng libtiff'],
    ['rankhisto_reg'        , 'gnuplot libjpeg libpng'],
    ['rasterop_reg'         , 'libtiff'],
    ['rasteropip_reg'       , 'libjpeg'],
    ['rectangle_reg'        , 'libpng libtiff'],
    ['rotate1_reg'          , 'libjpeg libpng'],
    ['rotate2_reg'          , 'libjpeg libpng'],
    ['rotateorth_reg'       , 'libjpeg libpng'],
    ['scale_reg'            , 'libjpeg libpng libtiff'],
    ['seedspread_reg'       , ''],
    ['selio_reg'            , ''],
    ['shear1_reg'           , 'libjpeg libpng'],
    ['shear2_reg'           , ''],
    ['skew_reg'             , 'libtiff'],
    ['smallpix_reg'         , ''],
    ['smoothedge_reg'       , 'libpng'                         , true],
    ['speckle_reg'          , 'libjpeg'],
    ['splitcomp_reg'        , ''],
    ['string_reg'           , ''],
    ['subpixel_reg'         , 'libjpeg libpng'],
    ['texturefill_reg'      , 'libjpeg'],
    ['threshnorm_reg'       , 'libjpeg'],
    ['translate_reg'        , 'libpng'],
    ['warper_reg'           , 'libtiff'],
    ['watershed_reg'        , 'libpng'],
    ['webpanimio_reg'       , 'libwebp_anim'],
    ['webpio_reg'           , 'libwebp'],
    ['wordboxes_reg'        , 'gnuplot libjpeg libpng libtiff'],
    ['writetext_reg'        , 'libjpeg libpng'],
    ['xformbox_reg'         , 'libtiff'],
  ]
    _name = _spec[0]
    _features = _spec[1]
    _manual_run = _spec.get(2, false)
    _extra_srcs = _spec.get(3, '').split()
    _missing_feats = []
    foreach _feat : _features != '' ? _features.split() : []
      if not get_variable('has_' + _feat)
        _missing_feats += _feat
      endif
    endforeach
    if _missing_feats.length() == 0
      _exe = executable(
        _name,
        [_name + '.c'] + _extra_srcs,
        c_args: '-DLIBLEPT_IMPORTS',
        dependencies: [lept_dep, _m_dep],
      )
    else
      _exe = executable(
        _name,
        '_skipped_test.c',
        c_args: '-DSKIP_REASON="' + 'missing feature(s): ' + ', '.join(
          _missing_feats,
        ) + '"',
      )
    endif
    if not _manual_run
      test(
        _name,
        _sh,
        args: ['-c', '"$1" generate && "$1" compare', '--', _exe],
        is_parallel: false,
        suite: 'reg',
        timeout: 60,
        workdir: meson.current_source_dir(),
      )
    endif
  endforeach

endif
